package com.fht.service.shop.impl;

import com.fht.entity.ShopAssistant;
import com.fht.from.ShopFrom;
import com.fht.mapper.shop.ShopAssistantMapper;
import com.fht.service.shop.ShopAssistantService;
import com.fht.util.UnitConversionUtil;
import com.fht.vo.LayUITableVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;


@Service
@Slf4j
public class ShopAssistantServiceImpl implements ShopAssistantService {

    @Resource
    public ShopAssistantMapper shopAssistantMapper;


    @Override
    public LayUITableVO<List<ShopAssistant>> findPage(Map<String, Object> map) {

        ShopFrom from = (ShopFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(ShopAssistant.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("merchantsId",from.getMerchantsId());

        if (! "".equals(from.getAssistantId()) && null!=from.getAssistantId()){
            c.andLike("id","%"+from.getAssistantId()+"%");
        }
        if (! "".equals(from.getAssistantName()) && null!=from.getAssistantName()){
            c.andLike("name","%"+from.getAssistantName()+"%");
        }

        List<ShopAssistant> list = shopAssistantMapper.selectByExample(example);

        for (ShopAssistant shopAssistant : list) {
            if (shopAssistant.getSex() == 0){
                shopAssistant.setSexString("女");
            }else {
                shopAssistant.setSexString("男");
            }

            if (shopAssistant.getStatus() == 0){
                shopAssistant.setStatusString("离职");
            }else {
                shopAssistant.setStatusString("在职");
            }
            shopAssistant.setEntryDate(UnitConversionUtil.conversionDate(shopAssistant.getEntryDate()));
        }

        PageInfo<ShopAssistant> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<ShopAssistant>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;

    }

    @Override
    public ShopAssistant selectById(String id) {
        ShopAssistant shopAssistant = shopAssistantMapper.selectByPrimaryKey(id);
        shopAssistant.setEntryDate(UnitConversionUtil.conversionDate(shopAssistant.getEntryDate()));
        return shopAssistant;
    }

    @Override
    @Transactional
    public void update(ShopAssistant shopAssistant) {
        shopAssistantMapper.updateByPrimaryKeySelective(shopAssistant);
    }

    @Override
    @Transactional
    public void save(ShopAssistant shopAssistant) {
        shopAssistantMapper.insertSelective(shopAssistant);
        ShopAssistant now = new ShopAssistant();
        now.setId(shopAssistant.getId());
//        System.out.println(new Md5Hash("123123","qwersofdandahfownviwneif",1024));
        now.setUsername(String.valueOf(shopAssistant.getId()));
        shopAssistantMapper.updateByPrimaryKeySelective(now);
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        shopAssistantMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ShopAssistant findByUsername(String username) {
        Example example = new Example(ShopAssistant.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("username",username);
        List<ShopAssistant> shopAssistants = shopAssistantMapper.selectByExample(example);
        shopAssistantMapper.findByUsername(username);
        return shopAssistants.get(0);
    }

    @Override
    public Set<String> findRolesByUsername(String username) {

        return shopAssistantMapper.findRolesByUsernaem(username);
    }

    @Override
    public Set<String> findPermissionsByRoles(Set<String> roleNames) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("set",roleNames);
        return shopAssistantMapper.findPermissionsByRoles(map);
    }

    @Override
    public LinkedHashMap<String, String> findShiroFilterChain() {

        return shopAssistantMapper.findShiroFilterChain();
    }

}
